package com.xbongbong.paas.lowcode.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.abstracts.AbstractLowCodeExecuteService;
import com.xbongbong.paas.help.HttpHelp;
import com.xbongbong.sys.help.PackageHelp;
import com.xbongbong.paas.lowcode.LowCodeExecuteService;
import com.xbongbong.paas.model.LowCodeServiceInfoModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.domain.entity.LowCodeServiceInfoEntity;
import com.xbongbong.pro.enums.PackageTypeEnum;
import com.xbongbong.pro.enums.errorcodes.ProErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.lowcodeexecute.pojo.dto.LowCodeExecuteRequestDTO;
import com.xbongbong.pro.packageInfo.pojo.vo.BasePackageInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/03/10 9:52
 */
@Service("lowCodeExecuteService")
public class LowCodeExecuteServiceImpl implements LowCodeExecuteService {

    private static final Logger LOG = LoggerFactory.getLogger(LowCodeExecuteServiceImpl.class);

    @Resource
    private HttpHelp httpHelp;
    @Resource
    private LowCodeServiceInfoModel lowCodeServiceInfoModel;
    @Resource
    private PackageHelp packageHelp;
    @Resource
    private AbstractLowCodeExecuteService abstractLowCodeExecuteService;

    @Override
    public String request(LowCodeExecuteRequestDTO lowCodeExecuteRequestDTO) throws XbbException {
        try {
            String corpid = lowCodeExecuteRequestDTO.getCorpid();
            BasePackageInfoVO basePackageInfoVO = packageHelp.getBasePackageInfoFromRedis(corpid);
            if (!Objects.equals(PackageTypeEnum.ULTIMATE.getType(), basePackageInfoVO.getFeeType())) {
                throw new XbbException(ProErrorCodeEnum.API_ERROR_260001);
            }
            LowCodeServiceInfoEntity lowCodeServiceInfoEntity = lowCodeServiceInfoModel.getByKey(lowCodeExecuteRequestDTO.getServiceId(), lowCodeExecuteRequestDTO.getCorpid());
            if (Objects.isNull(lowCodeServiceInfoEntity)) {
                throw new XbbException(ProErrorCodeEnum.API_ERROR_260029);
            }
            HttpMethod httpMethod = HttpMethod.resolve(lowCodeServiceInfoEntity.getRequestMethod());
            if (Objects.isNull(httpMethod)) {
                throw new XbbException(ProErrorCodeEnum.API_ERROR_260033);
            }
            JSONObject header = JSON.parseObject(lowCodeServiceInfoEntity.getRequestHeader());
            if (Objects.isNull(header)) {
                header = new JSONObject();
            }
            // TODO 测试用，要删掉
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = servletRequestAttributes.getRequest();
            header.put("sign", request.getHeader("sign"));

            String signature = lowCodeServiceInfoEntity.getSignature();
            JSONObject param = lowCodeExecuteRequestDTO.getParam();

            HttpHelp.setHeader(header, signature, lowCodeServiceInfoEntity.getHmacSecret(), httpMethod, param, lowCodeServiceInfoEntity.getSignatureFlag(), lowCodeServiceInfoEntity.getHmacSecretFlag());
            return httpHelp.send(header, param, lowCodeServiceInfoEntity.getUrl(), httpMethod, null);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("lowCodeExecuteService.request 出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }


}
